package com.srcpoint.common.tips.core
{
	
	import com.srcpoint.common.text.TextManager;
	import com.srcpoint.common.tips.TipManager;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	public class BaseTip extends Sprite implements ITip
	{
		public function BaseTip()
		{
			super();
			init();
		}
		protected var arrow:Shape;
		private var style:TipConf = new TipConf();
		private function init():void{
			arrow = new Shape();
			arrow.graphics.lineStyle(style.border_size,style.bg_color);
			arrow.graphics.beginFill(style.bg_color);
			arrow.graphics.moveTo(-style.arrawW/2,-style.arrawH);
			arrow.graphics.lineTo(style.arrawW/2,-style.arrawH);
			arrow.graphics.lineStyle(style.border_size,style.border_color);
			arrow.graphics.lineTo(0,0);
			arrow.graphics.lineTo(-style.arrawW/2,-style.arrawH);
			arrow.graphics.endFill();
			addChild(arrow);
		}
		protected var items:Array = [];	
		protected var direction:int;
		public function show(msg:XML, direction:int = 0, view:Object = null):void{
			this.direction = direction;
			if(msg.hasSimpleContent()){
				var tf:TextField = TextManager.instance.createDefaultTextField(msg);
				items.push(tf);
			}else{
				for each(var tip:XML in msg.child('tip')){
					createTipItem(tip);
				}
			}
			assemble();
		}
		
		/**
		 * 
		 * @param tip:xml has propety msg,type,itemtyle
		 * 
		 */		
		protected function createTipItem(tip:XML):void{
			var s:Sprite = new Sprite();
			var type:String = tip['@textType'];
			var tf:TextField = TextManager.instance.createDefaultTextField(tip,type);
			if(tip.hasOwnProperty('@tipType')){
				var tStyle:TipItemStyle = TipManager.instance.getTipItemStyle(tip['@tipType']);
				if(tStyle.hasBg){
					var shape:Shape = new Shape();
					shape.graphics.beginFill(tStyle.bgColor,tStyle.alpha);
					shape.graphics.drawRoundRect(0,0,tf.width+2*tStyle.margin,tf.height+2*tStyle.margin,tStyle.corner);
					shape.graphics.endFill();
					s.addChild(shape);
					tf.x = tStyle.margin;
					tf.y = tStyle.margin;
				}
			}
			s.addChild(tf);
			items.push(s);
		}
		
		protected function assemble():void{
			var d:DisplayObject;	
			var g:Sprite = new Sprite();
			for(var i:int=0;i<items.length;i++){
				d = items[i];
				d.y = (d.height+style.padding)*i+style.margin;
				d.x = style.margin;
				g.addChild(d);
			}
			var rec:Rectangle = g.getBounds(this);
			g.graphics.lineStyle(style.border_size,style.border_color,style.alpha);
			g.graphics.beginFill(style.bg_color,style.alpha);
			g.graphics.drawRoundRect(0,0,rec.width+2*style.margin,rec.height+2*style.margin,style.cornor,style.cornor);
			g.graphics.endFill();
			addChild(g);
			swapChildren(arrow,g);
			arrow.rotation = 90*direction;
			switch(direction){
				case TipConf.TOP://0					
					g.x = -g.width/2;
					g.y = 2*style.border_size-rec.height-arrow.height;
					break;				
				case TipConf.RIGHT://1
					g.x = style.arrawH;
					g.y = -g.height/2;
					break;				
				case TipConf.BOTTOM://2
					g.x = -g.width/2;
					g.y = style.arrawH;
					break;				
				case TipConf.LEFT://3
					g.x = style.border_size-g.width-style.arrawH;
					g.y = -g.height/2;
					break;
			}
		}
		
		public function hide():void{
		
		}
		
		protected var _invalid:Boolean;
		public function get invalid():Boolean{
			return _invalid;
		}
		
		public function set invalid(b:Boolean):void{
			_invalid = b;
		}
	}
}